/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package top.fsb.dataview.service.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.AllArgsConstructor;

import org.springframework.context.annotation.Primary;
import top.fsb.dataview.service.dto.VisualDbDTO;
import top.fsb.dataview.service.dynamic.DynamicDataSourceHolder;
import top.fsb.dataview.service.entity.VisualDb;
import top.fsb.dataview.service.mapper.VisualDbMapper;
import top.fsb.dataview.service.service.VisualDbService;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 可视化数据源配置表 服务实现类
 *
 * @author Chill
 */
@Service
@Primary
@AllArgsConstructor
public class VisualDbServiceImpl extends BaseServiceImpl<VisualDbMapper, VisualDb> implements VisualDbService {

	private final VisualDbMapper mapper;
	private final DynamicDataSourceHolder holder;

	@Override
	public Boolean dbTest(VisualDb db) {
		Connection conn = null;
		try {
			//测试驱动类
			Class.forName(db.getDriverClass());
			//创建连接
			conn = DriverManager.getConnection(db.getUrl(), db.getUsername(), db.getPassword());
			conn.setAutoCommit(Boolean.FALSE);
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		} finally {
			//关闭连接
			dbClose(conn);
		}
	}

	private void dbClose(Connection conn) {
		try {
			//关闭数据源连接
			if (conn != null) {
				conn.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public List<VisualDbDTO> dbList() {
		return mapper.dbList();
	}

	@Override
	public List<LinkedHashMap<String, Object>> dynamicQuery(String id, String sql) throws Exception {
		try {
			//处理自定义数据源
			holder.handleDataSource(id);
			//切换数据源
			DynamicDataSourceContextHolder.push(id);
//			//获取user信息
//			BladeUser user = AuthUtil.getUser();
//			//替换user占位符
//			String dynamicSql = PlaceholderUtil.getDefaultResolver().resolveByMap(sql, BeanUtil.toMap(user));
			//执行自定义sql
			return mapper.dynamicQuery(sql);
		} catch (Exception exception) {
			throw new Exception(exception.getMessage());
		} finally {
			//切回主数据源
			DynamicDataSourceContextHolder.poll();
		}

	}

}
